package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Activity;
import com.qk.mapper.ActivityMapper;
import com.qk.service.ActivityService;
import com.qk.dto.ActivityDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;
    @Override
    public PageResult<Activity> list(ActivityDto activityDto) {
        PageHelper.startPage(activityDto.getPage(),activityDto.getPageSize());
        List<Activity> activityList = activityMapper.selectList(activityDto);
        Page<Activity> activityPage = (Page<Activity>) activityList;
        return new PageResult<>(activityPage.getTotal(),activityPage.getResult());
    }

    @Override
    public void delete(Integer id) {
        activityMapper.delete(id);
    }

    @Override
    public Activity getActivityById(Integer id) {
        return activityMapper.getActivityById(id);
    }

    @Override
    public void add(Activity activity) {

        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.insert(activity);
    }

    @Override
    public void update(Activity activity) {
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.update(activity);
    }

    @Override
    public List<Activity> listByType(Integer type) {
        PageHelper.startPage(1,10);
        List<Activity> activityList = activityMapper.listByType(type);

        return activityList;
    }
}
